package com.yunkeji.api.user.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.api.index.bo.RankingListBo;
import com.yunkeji.api.index.bo.UserListBo;
import com.yunkeji.api.index.vo.*;
import com.yunkeji.api.invited.vo.InvitedVo;
import com.yunkeji.api.moments.vo.MomentsListVo;
import com.yunkeji.api.user.vo.CountVo;
import com.yunkeji.api.user.vo.FriendsList;
import com.yunkeji.dao.domain.UserFocus;
import com.yunkeji.dao.mapper.AppUserMapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * 用户
 *
 * @author 老张
 */
public interface ApiAppUserMapper extends AppUserMapper {

    /**
     * 首页用户列表
     *
     * @author 老张
     */
    @Select(
            "<script>"
                    + " SELECT users.id,users.platform_id,ext.nick_name,users.video_price,users.voice_price,users.disturb_flag,"
                    + " if(users.disturb_flag = 1 and users.online_status <![CDATA[>]]> 1, 3, users.online_status) online_status,"
                    + " users.card_auth,users.video_auth,ext.call_success,ext.call_fail,"
                    + " users.sex,"
                    + " ext.age,ext.head_url,ext.moments_pics,ext.album_pics,ext.voice_intro,"
                    + " ext.video_intro,ext.album_pics,ext.video_intro_cover_gif,ext.call_score,"
                    + " city.name cityName  "
                    + " from ykj_app_user users "
                    + " left join ykj_app_user_ext ext on ext.id=users.id "
                    + " left join ykj_sys_city city on city.id=ext.live_city "
                    + "where "
                    // 状态(0正常 1 封号 2删除 其他参考字典)
                    + " users.status=0 and (users.account_type=1 or users.account_type=2) "
                    // 不能查自己
                    + " and users.id!=#{bo.uid} "
                    // 爱洞id
                    + "<if test='bo.platformId!=null'>"
                    + " and users.platform_id=#{bo.platformId}"
                    + "</if>"
                    // 性别
                    + "<if test='bo.sex!=null'>"
                    + " and users.sex=#{bo.sex}"
                        //查询女性用户时，排序为0的用户将不再查询出来
                        + "<if test='bo.sex==1'>"
                        + " and users.sort <![CDATA[>]]> 0"
                        + "</if>"
                    + "</if>"
                    // 视频价格
                    + "<if test='bo.videoPriceMax!=null and bo.videoPriceMin!=null'>"
                    + " and users.video_price between #{bo.videoPriceMin} and #{bo.videoPriceMax} "
                    + "</if>"
                    // 年龄
                    + "<if test='bo.maxAge!=null and bo.minAge!=null'>"
                    + " and ext.age between #{bo.minAge} and #{bo.maxAge} "
                    + "</if>"
                    + " order by "
                    // 将在线状态和忙碌状态合并，优先排序展示
                    + " CASE "
                    + " WHEN online_status <![CDATA[>]]> 0 THEN "
                    + " 1 ELSE 0 "
                    + " END DESC, "
                    // 勿扰状态0未开启，1开启，开启勿扰的靠后
                    + " users.disturb_flag ASC, "
                    // 在线且没有开启勿扰的才会按照后台设置的排序进行倒序展示
                    + " users.sort DESC, "
                    // 已视频认证通过的优先展示
                    + " CASE "
                    + " WHEN video_auth = 2 THEN "
                    + " 1 ELSE 0  "
                    + " END DESC "
                    // 排序-女神或者男神 按照登录时间降序
                    + "<if test='bo.type==1'>"
                    + "   ,users.recently_access_time desc "
                    + "</if>"
                    // 排序-热门 按照最近通话时间降序
                    + "<if test='bo.type==2'>"
                    + "   ,ext.latest_call_date desc "
                    + "</if>"
                    // 排序-新人 按照注册时间降序userList
                    + "<if test='bo.type==3'>"
                    + "   ,ext.reg_date desc"
                    + "</if>"
                    + "</script>")
    Page<UserListVo> pageUserList(
            @Param("page") Page<MomentsListVo> page, @Param("bo") UserListBo bo);

    /**
     * 打招呼列表
     *
     * @author 老张
     */
    @Select(
            "<script>"
                    // 客户端没空改
                    + " SELECT users.platform_id id ,users.platform_id ,ext.nick_name ,"
                    + " ext.head_url "
                    + " from ykj_app_user users "
                    + " left join ykj_app_user_ext ext on ext.id=users.id "
                    + " left join ykj_sys_hello_detail detail on detail.rec_uid = users.platform_id "
                    // 只能打招呼未打招呼过的
                    + " AND detail.send_uid =#{bo.uid} and detail.send_uid is null "
                    + "where "
                    // 状态(0正常 1 封号 2删除 其他参考字典)
                    + " users.status=0 and users.account_type=1 "
                    // 在线状态 用户在线状态 0 离线 1 忙碌  2空闲 <4
                    + " and users.online_status !=0 "
                    // 不能查自己
                    + " and users.platform_id!=#{bo.uid} "
                    // 性别
                    + "<if test='bo.sex!=null'>"
                    + " and users.sex=#{bo.sex}"
                    + "</if>"
                    // 随机9个
                    + " ORDER BY RAND() LIMIT 9"
                    + "</script>")
    List<HelloListVo> helloList(@Param("bo") UserListBo bo);

    /**
     * 增加金币-统计累计充值成功次数，累计充值总数
     *
     * @author 老张
     * @param id
     */
    @Update(
            "UPDATE ykj_app_user_wallet SET charge_total = charge_total+#{charge_total},charge_num = charge_num+#{charge_num} WHERE id =#{id}")
    Integer addGoldCount(
            @Param("id") Long id,
            @Param("charge_total") BigDecimal chargeTotal,
            @Param("charge_num") Integer chargeNum);

    /**
     * 匹配在线用户
     *
     * @author 老张
     * @param code
     * @return
     */
    @Select(
            "<script>"
                    + "SELECT users.id,users.platform_id,ext.head_url,ext.nick_name"
                    + " FROM ykj_app_user users "
                    + " left join ykj_app_user_ext ext on ext.id= users.id "
                    // 性别 1= 女 0 = 男
                    + "<if test='sex == 0'>"
                    // 女匹配男需要男方的钱包余额大于等于女号设置的最低金额
                    + " LEFT JOIN ykj_app_user_wallet wallet on ext.id=wallet.id "
                    + "</if>"
                    + " where "
                    // 启用了视频速配
                    + "   users.enable_match=1 and "
                    // 状态(0正常 1 封号 2删除 其他参考字典)
                    + " users.status=0 and users.account_type=1 "
                    // 用户在线状态 0 离线 1 忙碌  2空闲
                    + " and users.online_status=2 "
                    + " and users.sex=#{sex} and "
                    + " users.id!=#{uid} "
                    // 性别 1= 女 0 = 男
                    + "<if test='sex == 0'>"
                    // 女匹配男需要男方的钱包余额大于等于女号设置的最低金额
                    + " and wallet.gold_balance &gt;= ${prize} "
                    + "</if>"
                    // 性别 1= 女 0 = 男
                    + "<if test='sex == 1'>"
                    // 男匹配女,需要女号的价格小于等于男号的余额
                    + " and users.video_price &lt;= ${prize} "
                    + "</if>"
                    + " ORDER BY RAND() LIMIT 1"
                    + "</script>")
    StartMatchVo startMatch(
            @Param("sex") Integer code, @Param("uid") Long uid, @Param("prize") BigDecimal prize);

    /**
     * 我的关注
     *
     * @author 老张
     * @param build
     * @param userId
     * @return
     */
    @Select(
            "SELECT "
                    + " ext.id, "
                    + " ext.nick_name, "
                    + " ext.head_url,focus.love_value,1 focusFlag "
                    + "FROM "
                    + " ykj_user_focus focus "
                    + " JOIN ykj_app_user_ext ext ON focus.focus_user_id = ext.id AND focus.fans_user_id = #{uid} and focus.del_flag=0  ")
    Page<FriendsList> myFocusfriendsList(
            @Param("page") Page<FriendsList> build, @Param("uid") Long userId);

    /**
     * 亲密度排序
     *
     * @author 老张
     * @param build
     * @param userId
     * @return
     */
    @Select(
            "SELECT "
                    + " ext.id, "
                    + " ext.head_url, "
                    + " ext.nick_name, "
                    + " focus.love_value  "
                    + "FROM "
                    + " ykj_user_focus focus  "
                    + "  JOIN ykj_app_user_ext ext ON focus.fans_user_id = ext.id  "
                    + "WHERE "
                    + " love_value != 0  "
                    + " AND focus.focus_user_id = #{uid} "
                    // 关注我的
                    + " UNION  " // 去重
                    // 我关注的
                    + "SELECT "
                    + " ext.id, "
                    + " ext.head_url, "
                    + " ext.nick_name, "
                    + " focus.love_value "
                    + "FROM "
                    + " ykj_user_focus focus  "
                    + "  JOIN ykj_app_user_ext ext ON focus.focus_user_id = ext.id  "
                    + "WHERE "
                    + " love_value != 0  "
                    + " AND focus.fans_user_id = #{uid}"
                    // 亲密度排序
                    + " order by love_value desc "
                    + "")
    Page<FriendsList> sortedLoveValue(
            @Param("page") Page<FriendsList> build, @Param("uid") Long userId);

    /**
     * 关注我的
     *
     * @author 老张
     * @param build
     * @param userId
     * @return
     */
    @Select(
            "SELECT "
                    + " ext.id, "
                    + " ext.nick_name, "
                    + " ext.head_url,focus.love_value  "
                    + "FROM "
                    + " ykj_user_focus focus "
                    + "  JOIN ykj_app_user_ext ext ON focus.fans_user_id = ext.id AND focus.focus_user_id = #{uid} and focus.del_flag =0  ")
    Page<FriendsList> friendsListFocusMe(
            @Param("page") Page<FriendsList> build, @Param("uid") Long userId);

    /**
     * 收益统计
     *
     * @author 老张
     * @param userId
     * @param format
     * @param countStr
     * @return
     */
    @Select(
            "SELECT "
                    + " SUM( change_num )changeNum, "
                    + " log_type  "
                    + "FROM "
                    + " ykj_pay_log  "
                    + " WHERE "
                    + " log_type IN ( ${countParam} )  "
                    + " AND symbol = '+'  "
                    + " AND balance_type = 2  "
                    + " AND log_date = #{date}  "
                    + " AND user_id = #{uid}  "
                    + "GROUP BY "
                    + " log_type")
    List<CountVo.CountPayLogData> countEarn(
            @Param("uid") Long userId,
            @Param("date") String format,
            @Param("countParam") String countStr);

    /**
     * 支付统计
     *
     * @author 老张
     * @param userId
     * @param format
     * @param countStr
     * @return
     */
    @Select(
            "SELECT "
                    + " SUM( change_num )changeNum, "
                    + " log_type  "
                    + "FROM "
                    + " ykj_pay_log  "
                    + " WHERE "
                    + " log_type IN ( ${countParam} )  "
                    //  + " AND symbol = '-'  "
                    + " AND log_date = #{date}  "
                    + " AND user_id = #{uid}  "
                    + "GROUP BY "
                    + " log_type")
    List<CountVo.CountPayLogData> countPay(
            @Param("uid") Long userId,
            @Param("date") String format,
            @Param("countParam") String countStr);

    @Select(
            "SELECT IFNULL(count(1),0) "
                    + " FROM "
                    + " ykj_app_user users "
                    + " LEFT JOIN ykj_sys_hello_detail detail ON users.platform_id = detail.rec_uid  "
                    + " AND detail.send_uid = #{platformId} "
                    + " WHERE "
                    // 用户在线状态 0 离线 1 忙碌  2空闲
                    + " users.online_status != 0  "
                    // 只能打招呼未打招呼过的
                    + " AND detail.rec_uid IS NULL  "
                    + " AND users.STATUS = 0  "
                    + " AND users.account_type = 1  "
                    + " AND users.platform_id != #{platformId}  "
                    + " AND users.sex = #{sex}  ")
    Integer countRandomMatch(@Param("platformId") Long platformId, @Param("sex") Integer sex);

    @Select(
            "SELECT "
                    + " users.id, "
                    + " users.platform_id, "
                    + " ext.nick_name, "
                    + " users.disturb_flag, "
                    + " users.online_status, "
                    + " users.card_auth, "
                    + " users.video_auth, "
                    + " users.recently_access_time, "
                    + " users.sex, "
                    + " ext.age, "
                    + " ext.head_url, "
                    + " detail.rec_uid  "
                    + "FROM "
                    + " ykj_app_user users "
                    + " LEFT JOIN ykj_app_user_ext ext ON ext.id = users.id "
                    + " LEFT JOIN ykj_sys_hello_detail detail ON users.platform_id = detail.rec_uid  "
                    + " AND detail.send_uid = #{platformId}  "
                    + "WHERE "
                    // 用户在线状态 0 离线 1 忙碌  2空闲
                    + " users.online_status != 0  "
                    // 只能打招呼未打招呼过的
                    + " AND detail.rec_uid IS NULL  "
                    + " AND users.STATUS = 0  "
                    + " AND users.account_type = 1  "
                    + " AND users.platform_id != #{platformId}  "
                    + " AND users.sex = #{sex}  "
                    + "ORDER BY "
                    + " RAND()  "
                    + " LIMIT ${num}")
    List<RandomMatchListVo> randomMatch(
            @Param("platformId") Long platformId,
            @Param("sex") Integer sex,
            @Param("num") Integer Num);

    /**
     * 邀请好友分页
     *
     * @author 老张
     * @param userId
     * @param build
     */
    @Select(
            "SELECT "
                    + " ext.nick_name, "
                    + " ext.head_url, "
                    + " users.platform_id,users.sex,users.create_time  "
                    + "FROM "
                    + " ykj_app_user users "
                    + " JOIN ykj_app_user_ext ext ON users.id = ext.id  "
                    + " AND users.pid =#{uid}")
    Page<InvitedVo> pageInvited(@Param("uid") Long userId, @Param("page") Page<Object> build);

    /**
     * online_status 用户在线状态 0 离线 1 忙碌 2空闲 status status(0正常 1 封号 2删除
     *
     * @author 老张
     */
    @Select(
            " select count( 1 ) FROM  ykj_app_user  WHERE  online_status =2 and sex=#{sex} and status=0")
    Integer countOnline(@Param("sex") Integer sex);

    @Select(
            " SELECT focus_user_id ,del_flag  from ykj_user_focus where fans_user_id= #{uid} and focus_user_id in (${ids})")
    /**
     * 传入id是否关注
     *
     * @author 老张
     */
    List<UserFocus> isFocus(@Param("ids") String ids, @Param("uid") Long uid);

    /**
     * 用户在线状态 0 离线 1 忙碌 2空闲 重置用户状态
     *
     * @author 老张
     */
    @Update(
            "UPDATE ykj_app_user SET online_status = #{afterStatus} WHERE id = #{uid} and online_status =#{beforeStatus}")
    void resetUserStatus(
            @Param("uid") Long uid,
            @Param("beforeStatus") Integer beforeStatus,
            @Param("afterStatus") Integer afterStatus);

    /**
     * 获取土豪排行榜
     *
     * @author 雷擎宇
     */
    @Select(
        "<script>"
            +"SELECT " +
            "d.user_id AS id," +
            "sum(d.cny_price) AS price, " +
            "e.head_url AS avatar, " +
            "e.nick_name AS nickname, " +
            "u.platform_id " +
            "FROM " +
            "ykj_pay_detail d " +
            "LEFT JOIN ykj_app_user u ON d.user_id = u.id " +
            "LEFT JOIN ykj_app_user_ext e ON d.user_id = e.id " +
            "WHERE " +
            "d. STATUS = 1 " +
            "GROUP BY " +
            "d.user_id, e.head_url, e.nick_name, u.platform_id " +
            "ORDER BY " +
            "price DESC"
            + "</script>")
    Page<RankingListVo> getWealthyRanking(@Param("page") Page<RankingListVo> buildPageQuery, @Param("bo") RankingListBo bo);

    /**
     * 获取邀请排行榜
     *
     * @author 雷擎宇
     */
    @Select(
        "<script>"
            + "SELECT " +
            "u.pid AS id, " +
            "count(u.id) AS invite_count, " +
            "e.head_url AS avatar, " +
            "e.nick_name AS nickname, " +
            "u2.platform_id " +
            "FROM " +
            "ykj_app_user u " +
            "LEFT JOIN ykj_app_user_ext e ON u.id = e.id " +
            "LEFT JOIN ykj_app_user u2 ON u2.id = u.pid " +
            "where u.pid is not null " +
            "GROUP BY " +
            "u.pid, e.head_url, e.nick_name, u2.platform_id " +
            "ORDER BY " +
            "invite_count DESC"
            + "</script>")
    Page<RankingListVo> getInviteRanking(@Param("page") Page<RankingListVo> buildPageQuery, @Param("bo") RankingListBo bo);
}
